package com.company.demo;

/**
 * 和第一种一样
 */
public class HungryMan02 {
    private static final HungryMan02 INSTANCE;

    /**
     * 静态代码块
     */
    static {
        INSTANCE = new HungryMan02();
    }

    public static void main(String[] args) {
        HungryMan02 m1 = HungryMan02.getSingletonObj();
        HungryMan02 m2 = HungryMan02.getSingletonObj();
        System.out.println(m1 == m2);
    }

    /**
     * 将构造方法设置成 private ，其他地方没法调用 构造方法
     */
    private HungryMan02() {
    }

    /**
     * 获取单例对象
     */
    public static HungryMan02 getSingletonObj() {
        return INSTANCE;
    }

}
